{include file="public/header"/}
<script src="{__PUBLIC_PATH}/common/js/jquery.js?v={$version}"></script>
<script src="{__PUBLIC_PATH}/admin/pcf/global.js?v={$version}"></script>
</head>
<body>
<style>
.layui-table-cell {height:30px!important;}
.undianji{pointer-events:none;background-color: #FBFBFB!important;border-color:#e6e6e6!important;color:#C9C9C9!important;}  
.arctype-btn-expand{float:left;margin:3px 6px 0 0;display:block;width:14px;height:14px;background:url(/common/images/ico_plus.png) center center;}
.arctype-btn-fold{float:left;margin:3px 6px 0 0;display:block;width:14px;height:14px;background:url(/common/images/ico_minus.png) center center;}
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">权限名称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入权限名称" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">菜单url:</label>
                        <div class="layui-input-inline">
                            <input name="url" class="layui-input" placeholder="输入路由地址"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">权限标识:</label>
                        <div class="layui-input-inline">
                            <input name="auth" class="layui-input" placeholder="输入权限标识"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="authoritiesTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button id="authoritiesAddBtn" class="layui-btn icon-btn" type="button" data-url="{:url('/system.menu/add')}">
                            <i class="layui-icon">&#xe654;</i>添加
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="authoritiesTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="authoritiesTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-sm {{d.status == 0 ? 'undianji':''}}" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm {{d.status == 0 ? 'undianji':''}}" lay-event="del" data-id="{{d.id}}">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="authoritiesEditDialog">
    <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
          style="padding-right:20px;">
        <input name="id" type="hidden"/>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单</label>
                    <div class="layui-input-block">
                        <div id="authoritiesEditParentSel" class="ew-xmselect-tree"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">权限名称:</label>
                    <div class="layui-input-block">
                        <input name="name" placeholder="请输入权限名称" class="layui-input" lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">权限类型:</label>
                    <div class="layui-input-block">
                        <select name="type" lay-verType="tips" lay-verify="required" required>
                            <option value="">选择类型</option>
                            <option value="1">模块</option>
                            <option value="2">导航</option>
                            <option value="3">菜单</option>
                            <option value="4">节点</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单url:</label>
                    <div class="layui-input-block">
                        <input name="url" placeholder="" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">状态</label>
                  <div class="layui-input-block">
                    <input type="checkbox" lay-filter="is_show" lay-skin="switch" lay-text="开启|关闭" name="is_show" checked>
                    <input type="hidden" name="is_show1" value="1">
                    <input type="hidden" name="is_show" value="">
                  </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">权限标识:</label>
                    <div class="layui-input-block">
                        <input name="auth" placeholder="" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单图标:</label>
                    <div class="layui-input-block">
                        <input name="icon" placeholder="" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">参数:</label>
                    <div class="layui-input-block">
                        <input name="param" placeholder="" type="text" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序号:</label>
                    <div class="layui-input-block">
                        <input name="sort" placeholder="" type="number" class="layui-input"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-center">
            <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<script type="text/html" id="tableState">
<input type="checkbox" data-id="{{d.id}}" name="status" value="{{d.is_show}}" lay-filter="status" lay-skin="switch" lay-text="正常|锁定" {{d.is_show == 1 ? 'checked':''}}>
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'admin', 'treeTable', 'util', 'xmSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var util = layui.util;
        var xmSelect = layui.xmSelect;
        var tbDataList = [];
        // 渲染表格
        var insTb = treeTable.render({
            elem: '#authoritiesTable',
            tree: {
                iconIndex: 1,
                idName: 'id',
                pidName: 'parent_id',
                isPidData: true
            },
            cellMinWidth: 100,
            cols: [
                {type: 'numbers'},
                {field: 'name', title: '权限名称',title: '<a href="javascript:;" class="arctype-btn-expand" id="btn-expand" style="display:none;"></a><a href="javascript:;" class="arctype-btn-fold" id="btn-fold"></a>菜单名称', width: 300},
                {title: '菜单图标', templet: '<p><i class="layui-icon {{d.icon}}"></i></p>', align: 'center', width: 100},
                {field: 'url', title: '菜单url'},
                {field: 'auth', title: '权限标识'},
                {field: 'param', title: '参数'},
                {field: 'sort', title: '排序号', align: 'center', width: 80},
                {title: '类型', templet: function (d) {
                    if(d.type == 1){
                        var strs = '<span class="layui-badge layui-badge-green">模块</span>';
                    }else if(d.type == 2){
                        var strs = '<span class="layui-badge layui-badge-blue">导航</span>';
                    }else if(d.type == 3){
                        var strs = '<span class="layui-badge layui-badge-gray">菜单</span>';                        
                    }else{
                        var strs = '<span class="layui-badge layui-badge-red">节点</span>';
                    }
                    return strs;
                }, align: 'center', width: 80},
                {field: 'is_show', templet: '#tableState', title: '状态',align: 'center',width: 100},
                {title: '创建时间', templet: '<p>{{layui.util.toDateString(d.createTime)}}</p>', align: 'center'},
                {title: '操作', templet: '#authoritiesTbBar', singleLine: false, align: 'center', width: 200}
            ],
            reqData: function (data, callback) {
                $.get("{:url('/system.menu/get_menu')}", function (res) {
                    tbDataList = res.data ? res.data : [];
                    callback(res.data);
                });
            }
        });

        $('#btn-expand').click(function () {
            $('#btn-expand').hide();
            $('#btn-fold').show();
            insTb.expandAll('#authoritiesTable');
        });

        $('#btn-fold').click(function () {
            $('#btn-expand').show();
            $('#btn-fold').hide();
            insTb.foldAll('#authoritiesTable');
        });
        
        // 表格操作列点击事件
        treeTable.on('tool(authoritiesTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        //开启关闭
        form.on('switch(status)', function(obj){
            if (obj.elem.checked){
                $(this).val(1);
            }else{
                $(this).val(0);
            }            
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/index/changeTableVal")}';
            changeTableVal('menu','id',dataid,'is_show',this,pcfurl,'Menu/index');
        });

        // 监听开关
        form.on('switch(is_show)', function(data){
          var elemId = data.elem.attributes['lay-filter']['nodeValue'];
          if (data.elem.checked) {
            this.value = 1;
          } else {
            this.value = 0;
          }
          $("input[name='"+elemId+"']").val(this.value);
          $("input[name='is_show1']").val(this.value);
        });

        // 添加
        $('#authoritiesAddBtn').click(function (mData) {
            showEditModel();
        });

        // 显示表单弹窗
        function showEditModel(mData) {
            admin.open({
                id:'pcfbg',
                type: 1,
                area: '600px',
                title: (mData ? '修改' : '添加') + '菜单',
                content: $('#authoritiesEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('authoritiesEditForm', mData);
                    // 表单提交事件
                    form.on('submit(authoritiesEditSubmit)', function (data) {
                        data.field.parentId = insXmSel.getValue('valueStr');
                        var loadIndex = layer.load(2);
                        $.post(mData ? "{:url('/system.menu/edit')}" : "{:url('/system.menu/add')}", data.field, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1, time: 1000});
                                insTb.refresh();
                            } else {
                                layer.msg(res.msg, {icon: 2, time: 1000});
                            }
                        }, 'json');
                        return false;
                    });
                    // 渲染下拉树
                    var insXmSel = xmSelect.render({
                        el: '#authoritiesEditParentSel',
                        height: '250px',
                        data: insTb.options.data,
                        initValue: mData ? [mData.parent_id] : [],
                        model: {label: {type: 'text'}},
                        prop: {
                            name: 'name',
                            value: 'id'
                        },
                        radio: true,
                        clickClose: true,
                        checkbox: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: true
                        }
                    });
                    // 弹窗不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }
        
        // 删除
        function doDel(obj) {
            layer.msg('确定要删除“' + obj.data.name + '”权限吗？', {
                btnAlign: 'c',
                time: 0,
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    layer.close(index);
                    var loadIndex = layer.load(2);
                    $.post("{:url('/system.menu/pcfdel')}", {id: obj.data.id}, function (res) {
                        layer.close(loadIndex);
                        if (res.code === 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000});
                            insTb.refresh();
                        } else {
                            layer.msg(res.msg, {icon: 2, time: 1000});
                        }
                    }, 'json');
                }
            });
        }

        // 表格搜索
        form.on('submit(authoritiesTbSearch)', function (data) {
            doTbSearch(data.field, 'id');
            return false;
        });

        // 搜索表格数据
        function doTbSearch(field, idName) {
            var ids = [], isClear = true;
            for (var i = 0; i < tbDataList.length; i++) {
                var item = tbDataList[i], flag = true;
                for (var f in field) {
                    if (!field.hasOwnProperty(f)) continue;
                    if (!field[f]) continue;
                    isClear = false;
                    if (!item[f] || item[f].indexOf(field[f]) === -1) {
                        flag = false;
                        break;
                    }
                }
                if (flag) ids.push(item[idName]);
            }
            if (isClear) {
                insTb.clearFilter();
            } else {
                insTb.filterData(ids);
            }
        }

    });
</script>
</body>
</html>